class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        nRow = len(matrix)
        nCol = len(matrix[0])
        def get(index):
            r = index//nCol
            c = index-r*nCol
            return matrix[r][c]
        begin, end = 0, nRow*nCol-1
        while begin<=end:
            mid = (begin+end)//2
            if get(mid)>target:
                end=mid-1
            elif get(mid)<target:
                begin=mid+1
            else:
                return True
        return False

matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

print(Solution().searchMatrix(matrix, 100))